package com.xiaoyg.config;

import com.rabbitmq.client.AMQP;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author xiaoyg
 * @Title: AnnotationConfiguration
 * @ProjectName spring-rabbitmq-zhu
 * @Description: 注解mq配置
 * @date 2018/12/710:57
 */

@Configuration
public class AnnotationConfiguration {

    protected String springQueueDemo = "spring-queue-demo";

    //创建连接
    @Bean
    public ConnectionFactory connectionFactory(){

        CachingConnectionFactory connectionFactory = new CachingConnectionFactory("192.168.8.129");
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("123456");
        connectionFactory.setPort(AMQP.PROTOCOL.PORT);

        return connectionFactory;
    }

    //创建rabbitAdmin 代理类
    @Bean
    public AmqpAdmin amqpAdmin(){
        return new RabbitAdmin(connectionFactory());
    }

    //创建rabbitTemplate 消息模板类
    @Bean
    public RabbitTemplate rabbitTemplate(){
        RabbitTemplate template = new RabbitTemplate(connectionFactory());
        template.setRoutingKey(springQueueDemo);
        template.setQueue(springQueueDemo);
        return template;
    }

    @Bean
    public Queue helloWorldQueue(){
        return new Queue(springQueueDemo);
    }

}
